<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link
            href="https://cdn.bootcss.com/bootstrap/3.0.0/css/bootstrap.min.css"
            rel="stylesheet">
    <title></title>
    <style type="text/css">
        .mask {
            position: absolute; top: 0px; filter: alpha(opacity=60); background-color: #777;
            z-index: 1002; left: 0px;
            opacity:0.5; -moz-opacity:0.5;
        }
    </style>
    <script type="text/javascript"
            src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.min.js"></script>
</head>
<body>

<!-- 列表展示操作 -->
<div class="col-md-12">
    <br/>
    <br/>
    <div style="font-weight: bold;font-size: large;text-align: center">定时调度管理</div>
    <br>
    <br/>
    <div id="taskInfo"></div>
    <!-- 遮罩层-->
    <div id="mask" class="mask">操作中,请稍后...</div>
</div>
</body>

<script type="text/javascript">

    function showMask(){
        $("#mask").css("height",$(document).height());
        $("#mask").css("width",$(document).width());
        $("#mask").show();
    }
    //隐藏遮罩层
    function hideMask(){
        $("#mask").hide();
    }

    function findAllTaskList(){

        showMask();
        $.ajax({
            type:'POST',
            contentType:'application/json',
            url:"/quartz/manager/list",
            success:function(data){
                if(data){
                    var result=data;
                    if(result&&result.code==200){
                        /***
                         * 进行数据表格的渲染操作
                         */
                        generateSchdulerInfo(result.data);
                    }
                }
                else{
                    alert("查询数据失败");
                }
                hideMask();
            },
            error:function(data){
                alert("服务器异常");
                hideMask();
            }
        });

    }


    /***
     * 先进行清空操作
     */
    function generateSchdulerInfo(data){
        $("#taskInfo").html("");
        var innerHtml="";
        if(!data){
            innerHtml="<p>暂无调度信息</p>";
        }else{
                var schedulerTaskInfo=data;
                var schedulerStatus=schedulerTaskInfo.status;
                //计划列表信息
                var planTaskList=schedulerTaskInfo.planTaskList;
                //已运行列表信息
                var runningTaskList=schedulerTaskInfo.runningTaskList;

                innerHtml+="<table class='table table-hover table-bordered'>";

                innerHtml+="<tr>";
                innerHtml+="<td colspan='2' style='font-weight: bold'>调度器:"+schedulerTaskInfo.schedulerName+"</td>";
                innerHtml+="<td colspan='3' style='font-weight: bold'>调度器状态:"+(schedulerStatus==0?"<b style='color: red'>已暂停</b>":schedulerStatus==1?"<b style='color: green'>已启动</b>":"<b style='color: red'>未启动</b>")+"</td>";
                innerHtml+="<td colspan='4' style='font-weight: bold'>操作:";

                if(schedulerStatus==0) {//已停止,或者是未启动
                    innerHtml += "<button class='btn btn-primary' onclick=startScheduler(\'"+schedulerTaskInfo.schedulerName+"\')>启动</button>";
                }else if(schedulerStatus==1) {//已启动
                    innerHtml+= "<button class='btn btn-primary' onclick=stopScheduler(\'"+schedulerTaskInfo.schedulerName+"\')>暂停</button>";
                }

                innerHtml+="</td>"
                innerHtml+="</tr>";


                //任务信息

                innerHtml+="<tr><td colspan='8' style='font-size: large;font-weight: bold;text-align: center'>运行中任务信息</td></tr>";
                innerHtml+=getTaskListData(runningTaskList,schedulerStatus,true);
                innerHtml+="<tr><td colspan='8' style='font-size: large;font-weight: bold;text-align: center'>计划中任务信息</td></tr>";
                innerHtml+=getTaskListData(planTaskList,schedulerStatus,false);

                innerHtml+="</table>";



        }

        $("#taskInfo").html(innerHtml);

    }


    function getTaskListData(data,schedulerStatus,running) {
        var innerHtml = "";
        innerHtml+="<tr>";
        innerHtml+=" <th>任务名称</th>";
        innerHtml+=" <th>任务组名称</th>";
        innerHtml+="  <th>触发器</th>";
        innerHtml+="  <th>上次执行时间</th>";
        innerHtml+=" <th>下次执行时间</th>";
        innerHtml+=" <th>Cron表达式</th>";
        innerHtml+=" <th>状态</th>";
        innerHtml+=" <th>操作</th>";
        innerHtml+="</tr>";

        if (!data) {
            innerHtml+="<tr><td colspan='8' align='center'>~~暂无数据</td></tr>";
        } else {
            for (var index = 0; index < data.length; index++) {
                var task = data[index];
                innerHtml += "<tr>";
                innerHtml += "<td>" + task.taskName + "</td>";
                innerHtml += "<td>" + task.groupName + "</td>";
                innerHtml += "<td>" + task.triggerName + "</td>";
                innerHtml += "<td>" + task.lastExecuteTime + "</td>";
                innerHtml += "<td>" + task.nextExecuteTime + "</td>";
                innerHtml += "<td>" + task.cronExpression + "</td>";
                innerHtml += "<td>" + task.statusName + "</td>";
                innerHtml += "<td>";
                if (task.statusName == "NORMAL") {

                    if(schedulerStatus==1) {
                        innerHtml += "<button class='btn btn-primary' onclick=pauseTask(\'"
                            + task.groupName + "\',\'"
                            + task.triggerName
                            + "\')>暂停</button>";
                        innerHtml+="&nbsp;&nbsp;<button class='btn btn-primary' onclick=runJobNow(\'"
                            + task.groupName + "\',\'"
                            + task.taskName
                            + "\')>执行</button>";
                        innerHtml+="&nbsp;&nbsp;<button class='btn btn-primary' onclick=delJob(\'"
                            + task.groupName + "\',\'"
                            + task.taskName
                            + "\')>删除</button>";
                    }else{
                        innerHtml += "<button class='btn btn-primary' disabled='disabled'  onclick=pauseTask(\'"
                            + task.groupName + "\',\'"
                            + task.triggerName
                            + "\')>暂停</button>";

                        innerHtml+="&nbsp;&nbsp;<button class='btn btn-primary'  disabled='disabled' onclick=runJobNow(\'"
                            + task.groupName + "\',\'"
                            + task.taskName
                            + "\')>执行</button>";
                        innerHtml+="&nbsp;&nbsp;<button class='btn btn-primary'  disabled='disabled' onclick=delJob(\'"
                            + task.groupName + "\',\'"
                            + task.taskName
                            + "\')>删除</button>";

                    }

                } else if (task.statusName == "PAUSED") {

                    if(schedulerStatus==1) {
                        innerHtml += "<button class='btn btn-primary' onclick=resumeTask(\'"
                            + task.groupName + "\',\'"
                            + task.triggerName
                            + "\')>恢复</button></td>";

                        innerHtml+="&nbsp;&nbsp;<button class='btn btn-primary' disabled='disabled' onclick=runJobNow(\'"
                            + task.groupName + "\',\'"
                            + task.taskName
                            + "\')>执行</button>";
                        innerHtml+="&nbsp;&nbsp;<button class='btn btn-primary' onclick=delJob(\'"
                            + task.groupName + "\',\'"
                            + task.taskName
                            + "\')>删除</button>";

                    }else{
                        innerHtml += "<button class='btn btn-primary' disabled='disabled' onclick=resumeTask(\'"
                            + task.groupName + "\',\'"
                            + task.triggerName
                            + "\')>恢复</button>";

                        innerHtml+="&nbsp;&nbsp;<button class='btn btn-primary' disabled='disabled' onclick=runJobNow(\'"
                            + task.groupName + "\',\'"
                            + task.taskName
                            + "\')>执行</button>";
                        innerHtml+="&nbsp;&nbsp;<button class='btn btn-primary' onclick=delJob(\'"
                            + task.groupName + "\',\'"
                            + task.taskName
                            + "\')>删除</button>";
                    }

                }
                innerHtml+"</td>";
                innerHtml += "</tr>";
            }

        }

        return innerHtml;
    };



    /***
     * 暂停任务
     */
    function pauseTask(groupName,triggerName){

        if(confirm("确认暂停该任务?")) {
            var key = groupName + "." + triggerName;
            $.ajax({
                type: 'GET',
                contentType: 'application/json',//注意类型
                url:'/quartz/manager/pause/'+ key+'/task',
                dataType: 'json',
                success: function (data) {
                    if (data) {
                        var result = data;
                        if (!result || result.code != 200) {
                            alert("操作失败");
                        }
                    } else {
                        alert("操作失败");
                    }
                    findAllTaskList();
                }
            });
        }
    }

    /**
     * 恢复任务执行
     */
    function resumeTask(groupName,triggerName){

        if(confirm("确认恢复该任务?")) {
            var key = groupName + "." + triggerName;
            $.ajax({
                type: 'GET',
                contentType: 'application/json',//注意类型
                url:  '/quartz/manager/resume/'+key+'/task',
                dataType: 'json',
                success: function (data) {
                    if (data) {
                        var result = data;
                        if (!result || result.code != 200) {
                            alert("操作失败");
                        }
                    } else {
                        alert("操作失败");
                    }
                    findAllTaskList();
                }
            });
        }
    }

    function startScheduler(schedulerName){
        if(confirm("确认启动调度器"+schedulerName)) {
            $.ajax({
                type: 'GET',
                contentType: 'application/json',//注意类型
                url: '/quartz/manager/start/scheduler',
                dataType: 'json',
                success: function (data) {
                    if (data) {
                        var result = data;
                        if (!result || result.code != 200) {
                            alert("操作失败");
                        }
                    } else {
                        alert("操作失败");
                    }
                    findAllTaskList();
                }
            });
        }
    }

    function stopScheduler(schedulerName){

        if(confirm("确认暂停调度器"+schedulerName+"?暂停将影响正在执行的任务.")) {
            $.ajax({
                type: 'GET',
                contentType: 'application/json',//注意类型
                url: '/quartz/manager/close/scheduler',
                dataType: 'json',
                success: function (data) {
                    if (data) {
                        var result = data;
                        if (!result || result.code != 200) {
                            alert("操作失败");
                        }
                    } else {
                        alert("操作失败");
                    }
                    findAllTaskList();
                }
            });
        }
    }


    function runJobNow(groupName,taskName){
        if(confirm("确认立即执行"+taskName+" 任务")) {
            $.ajax({
                type: 'GET',
                contentType: 'application/json',//注意类型
                url: '/quartz/manager/'+taskName+'/'+groupName+'/run',
                dataType: 'json',
                success: function (data) {
                    if (data) {
                        var result = data;
                        if (!result || result.code != 200) {
                            alert("操作失败");
                        }
                    } else {
                        alert("操作失败");
                    }
                    findAllTaskList();
                }
            });
        }
    }


    function delJob(groupName,taskName){
        if(confirm("确认立即执行"+taskName+" 任务")) {
            $.ajax({
                type: 'GET',
                contentType: 'application/json',//注意类型
                url: '/quartz/manager/'+taskName+'/'+groupName+'/delete',
                dataType: 'json',
                success: function (data) {
                    if (data) {
                        var result = data;
                        if (!result || result.code != 200) {
                            alert("操作失败");
                        }
                    } else {
                        alert("操作失败");
                    }
                    findAllTaskList();
                }
            });
        }
    }



    /***
     * 页面初始操作,进行数据的加载操作
     * 加载运行的任务和计划中的任务信息
     */
    $(function () {
        findAllTaskList();
    });


</script>

</html>

